package org.shan.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.social.connect.web.ProviderSignInUtils;
import org.springframework.social.security.SocialUserDetailsService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.ServletWebRequest;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by amanda.shan on 2019/3/19.
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private ProviderSignInUtils providerSignInUtils;

    @Autowired
    private SocialUserDetailsService userDetailsService;

    @GetMapping("/login")
    public String login() {
        return "login";
    }

    @GetMapping("/signup")
    public String sigup() {
        return "signup";
    }

    @PostMapping("/regist")
    public String regist(String username, String password, HttpServletRequest request) {

        //不管是注册用户还是绑定用户，都会拿到一个用户唯一标识。
        String userId = username;
        // 在这里就可以执行绑定或则注册用户的逻辑了
        // 然后使用 doPostSignUp 进行插入数据库
        providerSignInUtils.doPostSignUp(userId, new ServletWebRequest(request));

        return "redirect:/getAuthentication";
    }

}
